// vuex 状态管理
import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)

const store = new Vuex.Store({
	// 数据源
	state: {
		userinfo: uni.getStorageSync('USERINFO') || {},
		islogin: uni.getStorageSync("islogin") || false,
		socketTask: null,
		websocketData: {}, // 存放从后端接收到的websocket数据

	},
	mutations: {
		SET_USER_INFO(state, userInfo) {
			state.userinfo = userInfo
		},
		SET_islogin(state, islogin) {
			state.islogin = islogin
		},
		CLEAR_ISLOGIN(state) {
			state.islogin = false
		},
		setWebsocketData (state, data) {
		    state.websocketData = data
		}
	},
	actions: {
		set_userinfo({
			commit
		}, userinfo) {
			uni.setStorageSync('USERINFO', userinfo)
			commit('SET_USER_INFO', userinfo)
		},


		set_islogin({
			commit
		}, islogin) {
			uni.setStorageSync('islogin', islogin)
			commit('SET_islogin', islogin)
		},

		clearislogin({
			commit
		}) {
			uni.removeStorageSync('islogin')
			uni.removeStorageSync('USERINFO')
			commit('CLEAR_ISLOGIN')
		},
		
		websocketInit ({ state, dispatch }, url) {
		        state.socketTast = uni.connectSocket({
		            url, // url是websocket连接ip
		            success: () => {
		              
		            },
		            fail: e => {
		                console.log('连接失败' + e)
		            }
		        })
		        state.socketTast.onOpen(() => dispatch('websocketOnOpen'))
		        state.socketTast.onMessage(res => dispatch('websocketOnMessage', res))
		        state.socketTast.onClose(e => dispatch('websocketOnClose'))
		        state.socketTast.onError(e => dispatch('websocketOnError'))
		    },
		    websocketOnOpen ({ commit }) {
		        console.log('WebSocket连接正常打开中...！')
		    },
		    // 收到数据
		    websocketOnMessage ({ commit }, res) {
		        console.log('收到服务器内容：' + res.data)
		        if (res.data !== '连接成功') {
		            commit('setWebsocketData', (res && JSON.parse(res.data) || null))
		        }
		    },
		    websocketOnClose ({ commit, dispatch }) {
		        console.log('WebSocket连接关闭')
		    },
		    websocketOnError ({ commit, dispatch }) {
		        console.log('WebSocket连接错误')
		    },
		    websocketClose ({ state }) {
		        if (!state.socketTast) return
		        state.socketTast.close({
		            success (res) {
		                console.log('关闭成功', res)
		            },
		            fail (err) {
		                console.log('关闭失败', err)
		            }
		        })
		    },
		    // 发送数据
		    websocketSend ({ state }, data) {
		        uni.sendSocketMessage({
		            data :JSON.stringify(data),
		            success: res => {
		                console.log('发送成功', res)
		            },
		            fail: e => {
		                console.log('发送失败', e)
		            }
		        })
		    }
		


	}

})

export default store
